<?php

/**
 * Specify chapter object. Chapter table.
 */
class chapter
{
    /**
     * int chapter id
     */
    var $id;

    /**
     * int chapter book id
     */
    var $book;

    /**
     * int chapter number
     */
    var $number;

    /**
     * int chapter index
     */
    var $index;

    /**
     * int chapter level
     */
    var $level;

    /**
     * int (0, 1) is chapter visible
     */
    var $visible;

    /**
     * string chapter title
     */
    var $title;

    /**
     * string chapter icon
     */
    var $icon;

    /**
     * string chapter keywords
     */
    var $keywords;

    /**
     * string chapter text
     */
    var $text;

    /**
     * string chapter css
     */
    var $css;
}

/**
 * Import chapters for book_id from xml data file.
 *
 * @param $book_id int book id
 * @param $xml_data array
 *
 * @see get_xml_data()
 */
function import_chapters($book_id, $xml_data)
{
    $count = get_value_array($xml_data, 'SUNRAVBOOK/CHAPTERS/COUNT', 0);
    $chapters = array();
    for ($i = 0; $i < $count; $i++)
    {
        $chapter = new chapter();
        $chapter->book = $book_id;
        $chapter->number = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/NUMBER', 0);
        $chapter->index = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/INDEX', 0);
        $chapter->level = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/LEVEL', 0);
        $chapter->visible = ((get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/VISIBLE',
            'T') == 'T') ? 1 : 0);
        $chapter->title = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/TITLE');
        $chapter->icon = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/ICON');
        $chapter->keywords = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/KEYWORDS');
        $chapter->text = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/TEXT');
        $chapter->css = get_value_array($xml_data,
            'SUNRAVBOOK/CHAPTERS/CHAPTER_'.$i.'/CSS');
        add_chapter($chapter);
    }
}

/**
 * Add chapter.
 *
 * @param $chapter chapter class
 *
 * @return int chapter id on success or 0 on failure
 */
function add_chapter($chapter)
{
    $chapter->book      = db_prepare_int($chapter->book);
    $chapter->number    = db_prepare_int($chapter->number);
    $chapter->index     = db_prepare_int($chapter->index);
    $chapter->level     = db_prepare_int($chapter->level);
    $chapter->visible   = db_prepare_int($chapter->visible);
    $chapter->title     = db_prepare_string($chapter->title);
    $chapter->icon      = db_prepare_string($chapter->icon);
    $chapter->keywords  = db_prepare_string($chapter->keywords);
    $chapter->text      = db_prepare_string($chapter->text);
    $chapter->css       = db_prepare_string($chapter->css);

    $query = 'INSERT INTO '.DB_TABLE_CHAPTER.
            '(
            `chap_book_id`,
            `chap_number`,
            `chap_index`,
            `chap_level`,
            `chap_visible`,
            `chap_title`,
            `chap_icon`,
            `chap_keywords`,
            `chap_text`,
            `chap_css`
            )
        VALUES
            ('.
            $chapter->book.', '.
            $chapter->number.', '.
            $chapter->index.', '.
            $chapter->level.', '.
            $chapter->visible.', '.
            $chapter->title.', '.
            $chapter->icon.', '.
            $chapter->keywords.', '.
            $chapter->text.', '.
            $chapter->css.
            ')';

    $result = db_exec($query);

    if (db_last_error() != '')
    {
        return 0;
    }

    return db_insert_id();
}

/**
 * Delete chapters by book id.
 *
 * @param $p_book_id int book id
 *
 * @return bool TRUE on success or FALSE on failure
 */
function delete_chapters($p_book_id)
{
    $t_book_id = db_prepare_int($p_book_id);

    $query = 'DELETE FROM '.DB_TABLE_CHAPTER.
        ' WHERE `chap_book_id`='.$t_book_id;

    db_exec($query);
    return (db_last_error() == '');
}

/**
 * Get chapter by book id and chapter index.
 *
 * @param $p_book_id int book id
 * @param $p_chapter_index int chapter index
 *
 * @return chapter class
 */
function get_chapter_by_book_and_index($p_book_id, $p_chapter_index)
{
    $t_book_id = db_prepare_int($p_book_id);
    $t_chapter_index = db_prepare_int($p_chapter_index);
    $chapters[] = db_extract(DB_TABLE_CHAPTER,
        '`chap_book_id`='.$t_book_id.
        ' and `chap_index` ='.$t_chapter_index);
    $chapter = new chapter();
    if (count($chapters[0]) > 0)
    {
        $chapter->id = $chapters[0][0]['id'];
        $chapter->book = $chapters[0][0]['chap_book_id'];
        $chapter->number = $chapters[0][0]['chap_number'];
        $chapter->index = $chapters[0][0]['chap_index'];
        $chapter->level = $chapters[0][0]['chap_level'];
        $chapter->visible = $chapters[0][0]['chap_visible'];
        $chapter->title = $chapters[0][0]['chap_title'];
        $chapter->icon = $chapters[0][0]['chap_icon'];
        $chapter->keywords = $chapters[0][0]['chap_keywords'];
        $chapter->text = $chapters[0][0]['chap_text'];
        $chapter->css = $chapters[0][0]['chap_css'];
    }

    return $chapter;
}

/**
 * Get first chapter by book id.
 *
 * @param $p_book_id int book id
 *
 * @return chapter class
 */
function get_first_chapter_by_book_id($p_book_id)
{
    $t_book_id = db_prepare_int($p_book_id);

    $chapters[] = db_extract(DB_TABLE_CHAPTER,
                 '`chap_book_id`='.$t_book_id,
                 '`chap_index` ASC', '0, 1'
                 );

    $chapter = new chapter();
    if (count($chapters[0]) > 0)
    {
        $chapter->id = $chapters[0][0]['id'];
        $chapter->book = $chapters[0][0]['chap_book_id'];
        $chapter->number = $chapters[0][0]['chap_number'];
        $chapter->index = $chapters[0][0]['chap_index'];
        $chapter->level = $chapters[0][0]['chap_level'];
        $chapter->visible = $chapters[0][0]['chap_visible'];
        $chapter->title = $chapters[0][0]['chap_title'];
        $chapter->icon = $chapters[0][0]['chap_icon'];
        $chapter->keywords = $chapters[0][0]['chap_keywords'];
        $chapter->text = $chapters[0][0]['chap_text'];
        $chapter->css = $chapters[0][0]['chap_css'];
    }

    return $chapter;
}

